package com.bort.system;

import com.bort.system.view.IndexView;
import de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collection;

@EnableScheduling
@SpringBootApplication
public class BortApplication extends AbstractJavaFxApplicationSupport {

    public static void main(String[] args) {
        launch(BortApplication.class, IndexView.class,args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        //设置主题
        stage.setTitle("睿美台球管理系统");
        super.start(stage);
    }

    @Override
    public Collection<Image> loadDefaultIcons() {
        return Arrays.asList(new Image("icon/logo.png"));
    }


    public static void stopProcessByPort(int port) {
        try {

            String os = System.getProperty("os.name").toLowerCase();
            String command = "";

            if (os.contains("win")) {
                command = "\"netstat -ano | findstr " + port+"\"";
            } else {
                command = "netstat -tulnp | grep \"LISTEN\" | grep \":" + port + "\"";
            }
            ProcessBuilder builder = new ProcessBuilder(command);
            Process process = builder.start();

            // 读取进程ID
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                String killCommand = "kill -9 " + line;
                Runtime.getRuntime().exec(killCommand);
            }

            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
